//位置控制
import * as THREE from "../node_modules/three/build/three.module.js";
import { OrbitControls } from "../node_modules/three/examples/jsm/controls/OrbitControls.js";
import { GLTFLoader } from "../node_modules/three/examples/jsm/loaders/GLTFLoader";



var scene, camera, renderer, controls, loader

function init() {
    scene = new THREE.Scene()
    camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000)
    camera.position.set(0, 0, 30)
    camera.lookAt(new THREE.Vector3(0, 0, 0))
    scene.add(camera)
    renderer = new THREE.WebGLRenderer()
    renderer.setSize(window.innerWidth, window.innerHeight)
    document.body.appendChild(renderer.domElement)

    var light = new THREE.AmbientLight(0x404040, 8); // soft white light

    var group = new THREE.Group();
    group.add(light);

    //加载模型
    loader = new GLTFLoader();
    loader.load(
        "../src/models/viking_room/scene.gltf",
        function (gltf) {
            let model = gltf.scene
            // model.position.set(0, 4, 0)
            // model.lookAt(0, 4, 1)
            // model.rotation.z = -Math.PI / 2
            group.add(model);
            scene.add(group);

        },
        undefined,
        function (error) {
            console.log(error);
        }
    );




    controls = new OrbitControls(camera, renderer.domElement);
    controls.update();

    var light = new THREE.AmbientLight(0x404040); // soft white light
    scene.add(light);
}



function reference() {
    //参考系
    var material1 = new THREE.LineBasicMaterial({ color: 0xd63031 }); //红
    var material2 = new THREE.LineBasicMaterial({ color: 0x55efc4 }); //绿
    var material3 = new THREE.LineBasicMaterial({ color: 0x0984e3 }); //蓝
    var geometry1 = new THREE.Geometry();
    var geometry2 = new THREE.Geometry();
    var geometry3 = new THREE.Geometry();
    let l = 100
    geometry1.vertices.push(new THREE.Vector3(0, 0, 0));
    geometry2.vertices.push(new THREE.Vector3(0, 0, 0));
    geometry3.vertices.push(new THREE.Vector3(0, 0, 0));
    geometry1.vertices.push(new THREE.Vector3(l, 0, 0));
    geometry2.vertices.push(new THREE.Vector3(0, l, 0));
    geometry3.vertices.push(new THREE.Vector3(0, 0, l));
    var line1 = new THREE.Line(geometry1, material1);
    var line2 = new THREE.Line(geometry2, material2);
    var line3 = new THREE.Line(geometry3, material3);
    scene.add(line1);
    scene.add(line2);
    scene.add(line3);
}

function animate() {
    requestAnimationFrame(animate)
    renderer.render(scene, camera);

}

init()
reference()
animate()